package view;

import javax.swing.JProgressBar;
import javax.swing.event.DocumentEvent.EventType;

import eventHandling.EventBus;
import eventHandling.EventTyp.Type;

/**
 * This class updates the value of a JProgressBar each second until the maxTime
 * is reached.
 * 
 * @author sloth42
 * 
 */
public class SecondCounterThread extends Thread {

	private EventBus eventBus;
	private Type eventType;
	private int maxTime;
	private JProgressBar progressBar;

	/**
	 * 
	 * @param progressBar JProgressBar to update
	 * @param maxTime maximum time in seconds
	 */
	public SecondCounterThread(EventBus eventBus, JProgressBar progressBar, int maxTime, Type eventType) {
		this.eventBus = eventBus;
		this.eventType = eventType;
		this.progressBar = progressBar;
		this.maxTime = maxTime;
		progressBar.setMaximum(maxTime);
	}

	@Override
	public void run() {
		progressBar.setStringPainted(true);
		while (progressBar.getValue() < maxTime) {
			progressBar.setValue(progressBar.getValue() + 1);
			progressBar.setString(progressBar.getValue() + "/" + maxTime + "Sekunden");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		eventBus.fireEvent(eventType);
	}

}
